package scoutobahn.xml;
/**
 * Lazy OSM item, saves only the id of the given OSM item
 * and retrieves it later.
 * @author mark
 *
 */
public class LazyOsmItem extends OsmItem {

    private final OsmItemProvider provider;
    private final OsmXmlTypes type;
    /**
     * Construct's new LazyOsmItem with the given id and a OsmItemProvider.
     * @param newId The id of the represented item
     * @param itemProvider The OsmItemProvider to retrieve the item.
     * @param osmType The type of this lazy node
     */
    public LazyOsmItem(final long newId,
            final OsmItemProvider itemProvider, final OsmXmlTypes osmType) {
        super(newId, null);
        this.type = osmType;
        this.provider = itemProvider;
    }
    /**
     * Return's weather the OsmItemProvider would be able to return the item.
     * @return Weather the item is already loaded
     */
    public boolean isInitialized() {
        return this.provider != null
                && this.provider.hasItem(super.getId(), this.type);
    }
    /**
     * Gets the real item.
     * @return The real item.
     */
    public OsmItem getRealItem() {
        if (this.provider == null) { return null; }
        return this.provider.getItemById(super.getId(), this.type);
    }
}
